package com.product.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.common.R;
import com.product.controller.vo.editEquityVo;
import com.product.controller.vo.equityVo;
import com.product.service.IProductEquity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/product")
public class equityController {

    @Autowired
    private IProductEquity productEquity;



    @SaCheckPermission("product:edit")
    @SaCheckRole(value = {"manager", "super_admin"}, mode = SaMode.OR)
    @PostMapping("/edit")
    public R<Object> editEquity(@Validated @RequestBody editEquityVo equityVo) {

        return productEquity.editEquity(equityVo) ? R.data(true) : R.fail("商品编辑失败");

    }
    /*
     * @author 刘童
     * @date 2024/6/19
     * 根据权益商品Id查询商品
     */

    @SaCheckPermission("product:get")
    @GetMapping("/productInfo")
    public R<editEquityVo> getEquityByEquityId(@RequestParam("equity_id") String equityId) {


        long equityIdValue;
        try {
            // 验证equityId是否为空且尝试将其转换为Long，以防止NumberFormatException
            if (equityId != null && !equityId.trim().isEmpty()) {
                equityIdValue = Long.parseLong(equityId.trim());
            } else {
                return R.fail("equityId不能为空");
            }
        } catch (NumberFormatException e) {
            // 处理equityId不是一个有效数字的情况
            return R.fail("equityId格式无效");
//            throw new IllegalArgumentException("EquityID 格式无效。请提供有效的数值", e);
        }

        // 确保getEquityByEquityId方法的实现能够妥善处理null或异常情况
        editEquityVo editEquityVo = productEquity.getEquityByEquityId(equityIdValue);
        if (editEquityVo == null) {
            // 这里可以根据实际需求决定如何处理找不到对应equity的情况，比如抛出异常或返回特定错误信息
            return R.fail("未找到商品信息");
        }

        return R.data(editEquityVo);


//        return R.data(productEquity.getEquityByEquityId(Long.parseLong(equityId)));
    }


}
